package com.iss.ljx.user.controller;

import com.alibaba.fastjson.JSON;
import com.iss.ljx.mybatis.entity.PtUserEntity;
import com.iss.ljx.mybatis.flexigrid.PadingRstType;
import com.iss.ljx.mybatis.flexigrid.PagingBean;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.spring.controller.BaseController;
import com.iss.ljx.user.entity.SearchUserEntity;
import com.iss.ljx.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * @author 流星
 */
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;
    @RequestMapping("/queryUserListByName")
    @ResponseBody
    public List<PtUserEntity> queryUserListByName(SearchUserEntity search){
        log.info("****{}",userService.queryUserListByName(search));
        return userService.queryUserListByName(search);
    }
    @Resource
    private MessageSource messageSource;

    @RequestMapping("/deleteUserInfoById")
    @ResponseBody
    public String deleteUserInfoById(String userUuid){
        boolean r=userService.deleteUserInfoById(userUuid);
        HashMap<String,Object> message = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr="";
        if(!r){
            msgStr = messageSource.getMessage("user.del.defeat", null, locale);
        }else{
            msgStr = messageSource.getMessage("user.del.success", null, locale);
        }
        message.put("success", true);
        message.put("msg", msgStr);
        message.put("description", msgStr);
        String rst = JSON.toJSONString(message);
        return rst;
    }
    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/userPage")
    public String userPage(){

        return "user/userPage";
    }

    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/getUserInfolistByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> getUserInfolistByPage(SearchUserEntity search, PagingBean paging){
        log.info("{}",search);
        PadingRstType<PtUserEntity> ptUserEntityPadingRstType = userService.getUserInfolistByPage(search,paging);
        return  ptUserEntityPadingRstType;
    }
    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/getRoleZtreeListByUserId")
    @ResponseBody
    public List<ZtreeEntity> getRoleZtreeListByUserId(@RequestParam("userUuid") String userUuid){
        List<ZtreeEntity> list = userService.getRoleZtreeListByUserId(userUuid);
        return list;
    }
    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/allocateRoleByUser")
    @ResponseBody
    public String allocateRoleByUser(@RequestParam("userUuid") String userUuid,
                                     @RequestParam("roleArray") String roleArray){
        userService.allocateRoleByUser(userUuid,roleArray);
        return getSuccessMsg("user.allocat.role.success",null);
    }


    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/insertUser")
    @ResponseBody
    public String insertUser(PtUserEntity ptUser){
        userService.insertUser(ptUser);
        return getSuccessMsg("user.insert.success",null);
    }

    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(PtUserEntity ptUser){
        //log.info("{}",ptUser);
        boolean r=userService.updateUser(ptUser);
        if (r){
            return getSuccessMsg("user.update.success",null);
        }else{
            return getSuccessMsg("user.update.failure",null);
        }
    }
}
